package tests.authentication;

import static org.junit.Assert.*;

import java.io.IOException;
import org.junit.Before;
import org.junit.Test;

import authentication.UserAuthentication;
//The testing counts on the correctness of the Sun security package
public class UserAuthenticationTest {
	UserAuthentication ua;
	String s1 = "s1";
	String s2 = "s2";
	String s3 = "s3";
	String stub = "stub";
	String ps1 = "p1";
	String ps2 = "p2";
	String ps3 = "p3";
	String ad = "ADMIN";
	String us = "USER";
	String mb = "MEMBER";
	@Before
	public void setup() throws IOException{
	ua = new UserAuthentication("usersTest.txt");
	ua._users_file.delete();
	ua._users_file.createNewFile();
	}
	
	@Test
	public void testContainsUser() throws Exception {
		assertFalse(ua.containsUser(s1));
		assertFalse(ua.containsUser(s2));
		assertFalse(ua.containsUser(s3));
		ua.addUser(s1,stub,stub);
		assertTrue(ua.containsUser(s1));
		assertFalse(ua.containsUser(s2));
		assertFalse(ua.containsUser(s3));
		ua.addUser(s3, stub, stub);
		assertTrue(ua.containsUser(s3));
		assertTrue(ua.containsUser(s1));
		assertFalse(ua.containsUser(s2));
		
	}
		
	@Test
	public void testCheckUser() throws Exception {
		assertFalse(ua.checkUser(s1, ps1));
		assertFalse(ua.checkUser(s2, ps2));
		assertFalse(ua.checkUser(s3, ps3));
		ua.addUser(s2, ps2, ad);
		assertFalse(ua.checkUser(s1, ps1));
		assertFalse(ua.checkUser(s2, ps3));
		assertTrue(ua.checkUser(s2, ps2));
		assertFalse(ua.checkUser(s3, ps3));
		ua.addUser(s3, ps3, us);
		assertTrue(ua.checkUser(s2, ps2));
		assertFalse(ua.checkUser(s1, ps1));
		assertTrue(ua.checkUser(s3, ps3));
	}

	@Test
	public void testAddUser() throws Exception {
		assertFalse(ua.containsUser(s1));
		assertFalse(ua.containsUser(s2));
		assertTrue(ua.addUser(s1, ps1, ad));
		assertTrue(ua.containsUser(s1));
		assertTrue(ua.addUser(s2, ps2, ad));
		assertTrue(ua.containsUser(s2));
		assertFalse(ua.addUser(s1, ps1, us));
		assertFalse(ua.addUser(s2, ps3, ad));
		assertFalse(ua.addUser(s1, ps1, ad));
	}

	@Test
	public void testDeleteUser() throws Exception {
		assertFalse(ua.containsUser(s1));
		assertFalse(ua.containsUser(s2));
		assertFalse(ua.containsUser(s3));
		ua.addUser(s1, ps1, ad);
		assertTrue(ua.containsUser(s1));
		ua.addUser(s2, ps2, ad);
		assertTrue(ua.containsUser(s2));
		assertTrue(ua.deleteUser(s2));
		assertFalse(ua.containsUser(s2));
		assertTrue(ua.containsUser(s1));
		assertFalse(ua.containsUser(s3));
		assertFalse(ua.deleteUser(s2));
		assertTrue(ua.deleteUser(s1));
		assertFalse(ua.containsUser(s1));
	}

	@Test
	public void testGetUserPermission() throws Exception {
		ua.addUser(s1, ps1, ad);
		assertTrue(ua.getUserPermission(s1).equals(ad));
		assertEquals(ua.getUserPermission(s1),"ADMIN");
		ua.addUser(s2,ps2,stub);
		assertTrue(ua.getUserPermission(s2)==null);
		assertFalse(ua.getUserPermission(s1).equals(mb));
		ua.addUser(s3,ps3,mb);
		assertEquals(ua.getUserPermission(s3),mb);
	}

}
